#pragma once
#include <QWidget>
#include <QFrame>
#include <QPushButton>
#include <QGraphicsDropShadowEffect>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QMouseEvent>
#include <QProcess>
#include <QEventLoop>
#include "messageinfomation.h"
#include <QLabel>

#define CONFIG_GENERAL      "general_config"
#define CONFIG_PRODUCT_NAME      "product_name"
#define CONFIG_PRODUCT_REGION   "product_region"
#define CONFIG_PRODUCT_PROCESS_NAME      "product_process_name"
#define CONFIG_INSTALL_PROGRAM_NAME      "install_program_name"
#define CONFIG_PRODUCT_VERSION      "product_version"
#define CONFIG_PACKAGE_SIZE      "package_size"
#define CONFIG_INSTALL_DEFAULT_PATH     "install_default_path"
#define CONFIG_INSTALL_LICENSE_URL     "install_license_url"
#define CONFIG_APP_ID     "product_app_id"

extern QString gInstallDir;
extern QString gInstallDirCN;


QT_BEGIN_NAMESPACE
namespace Ui { class BkWidget; }
QT_END_NAMESPACE

class BkWidget : public QWidget
{
    Q_OBJECT
public:
    explicit BkWidget(QWidget *parent = nullptr);
    ~BkWidget();

    void initConnection();
    void initSettings();
    void initData();
    void initView();

public slots:
    void onInstallProgram();
    void onRunProgram();
    void onFinished(int exitCode, QProcess::ExitStatus exitStatus);
    void onCheckProgram(const int msgType, int closeType);

protected:
    void mousePressEvent(QMouseEvent *event) override;
    void mouseMoveEvent(QMouseEvent *event) override;
    void mouseReleaseEvent(QMouseEvent *event) override;
    bool nativeEvent(const QByteArray &eventType, void *message, long *result) override;
    bool eventFilter(QObject *obj, QEvent *event) override;

private:
    void showBrowserControl();
    bool getStorageInfo();
    QString calculateSize(const qint64 &size);
    qint64 calculateAvailableSize(const QString& path);
    bool dealSelectPath(const QString& path);
    bool isProcessRunning();
    int checkExsitInstallPath();
    bool checkInstallProgram();
    void writeReg();
    bool execInstall();
    void showShadow();
    void setInstallState();

    bool m_dragging = false;
    QPoint m_dragPosition;
    Ui::BkWidget *ui;
    QString m_productName = "";
    QString m_productProcessName = "";
    int m_productRegion = 0;
    QString m_installProgramName = "";
    QString m_productVersion = "";
    QString m_installDefaultPath = "";
    QString m_installPath = "";
    QString m_installLicenseUrl = "";
    QString m_isntallAppId = "";
    quint64 m_packageSize = 0;
    bool m_bBrowser = false;
    QProcess* m_process = nullptr;
    quint64 m_totalSize = 0;
    quint64 m_currentSize = 0;
    MessageInfomation* m_messageBox = nullptr;
    MessageInfomation::emCloseType m_mssageCloseType = MessageInfomation::kClose;
    QEventLoop m_loop;
    QFrame *m_shadowFrame = nullptr;
    QLabel* m_shadowLabel = nullptr;
};
